package cn.hk.user.service.impl;

import cn.hk.archives.domain.ArchivesChild;
import cn.hk.archives.domain.ArchivesCommon;
import cn.hk.archives.domain.ArchivesGravida;
import cn.hk.archives.domain.ArchivesOld;
import cn.hk.archives.domain.vo.ArchivesVo;
import cn.hk.archives.service.ArchivesChildService;
import cn.hk.archives.service.ArchivesCommonService;
import cn.hk.archives.service.ArchivesGravidaService;
import cn.hk.archives.service.ArchivesOldService;
import cn.hk.common.entity.UserType;
import cn.hk.common.property.WeixinProperties;
import cn.hk.common.utils.SaltMD5Util;
import cn.hk.common.utils.WxDecryptUtills;
import cn.hk.user.domain.UserInfo;
import cn.hk.user.domain.vo.FamilyPersonVo;
import cn.hk.user.domain.vo.WxLoginBodyVo;
import cn.hk.user.domain.vo.WxLoginUserInfoVo;
import cn.hk.user.mapper.UserInfoMapper;
import cn.hk.user.service.UserFamilyService;
import cn.hk.user.service.UserInfoService;
import cn.hk.user.service.UserRelationService;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Console;
import cn.hutool.core.util.IdcardUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.service.TokenService;
import nonapi.io.github.classgraph.json.Id;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * app用户表 服务实现类
 * </p>
 *
 * @author xu
 * @since 2023-03-07
 */
@Service

public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements UserInfoService {

    @Autowired
    private WeixinProperties weixinProperties;

    @Autowired
    private TokenService tokenService;
    @Autowired
    private UserFamilyService userFamilyService;

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private UserRelationService userRelationService;

    @Autowired
    private ArchivesCommonService archivesCommonService;

    @Autowired
    private ArchivesChildService archivesChildService;

    @Autowired
    private ArchivesGravidaService archivesGravidaService;

    @Autowired
    private ArchivesOldService archivesOldService;

    /**
     * 添加家庭成员
     * @param familyPersonVo
     * @return
     */
    @Transactional
    @Override
    public AjaxResult addFamilyMember(FamilyPersonVo familyPersonVo) {
        UserInfo userInfo = null;
             userInfo = new UserInfo();
             //判断身份证号是否存在
             if (ObjectUtil.isNotNull(familyPersonVo.getIdCard())){
                 UserInfo userByIdCard  = userInfoMapper.selectOneByIdCard(familyPersonVo.getIdCard());
                 if (!BeanUtil.isEmpty(userByIdCard)){
                     return AjaxResult.error("身份证号已存在请重新输入");
                 }
             }
            //登录手机号为空
            if ( ObjectUtil.isEmpty(familyPersonVo.getMobile())){
                //添加者信息
                UserInfo userFrom = userInfoMapper.selectOneById(familyPersonVo.getFromUserId());
                //新增用户
                UserInfo user = creatUserInfo(familyPersonVo,userFrom);
                if (BeanUtil.isEmpty(user)){
                    return AjaxResult.error("添加失败").put("msg", "添加失败");
                }
                //新增用户信息
                userInfoMapper.insertUser(user);
                //新增用户档案信息
                addFamilyArchives(user,userFrom);
                BeanUtil.copyProperties(user,userInfo);
            }else {
                //登录手机号不为空
                //判断添加用户是否存在
                UserInfo user = userInfoMapper.selectUserByPhone(familyPersonVo.getMobile());
                if(BeanUtil.isEmpty(user)){
                    //添加者信息
                    UserInfo userFrom = userInfoMapper.selectOneById(familyPersonVo.getFromUserId());
                    //用戶不存在 新增用户
                    userInfo = creatUserInfo(familyPersonVo,userFrom);
                    if (BeanUtil.isEmpty(userInfo)){
                        return AjaxResult.error("添加失败").put("mag", "添加失败");
                    }
                    //新增用户信息
                    userInfoMapper.insertUser(userInfo);
                    //新增用户档案信息
                    addFamilyArchives(user,userFrom);
                    BeanUtil.copyProperties(user,userInfo);
                }else {
                    //用户已存在
                    return AjaxResult.error("该手机号已被注册,请输入其他手机号");
                }
            }

            //添加家庭关系
            userRelationService.addUserRelation(familyPersonVo,userInfo);
            //添加家庭成员
            userFamilyService.addUserFamily(familyPersonVo,userInfo);

        return AjaxResult.success("添加成功").put("item",userInfo);
    }



    /**
     * 添加用戶信息
     * @param familyPersonVo
     * @return
     */
    UserInfo creatUserInfo(FamilyPersonVo familyPersonVo,UserInfo userFrom) {

        UserInfo userInfo = null;
        if (BeanUtil.isNotEmpty(familyPersonVo)){
            userInfo = new UserInfo();
            //身份证不为空且医学出生证明为空则生日和性别根据身份证得出
            if(ObjectUtil.isNotEmpty(familyPersonVo.getIdCard()) && ObjectUtil.isEmpty(familyPersonVo.getBirthCertificate()) ){
                userInfo.setIdCard(familyPersonVo.getIdCard());
                userInfo.setBirthday(IdcardUtil.getBirthDate(familyPersonVo.getIdCard()));
                userInfo.setGender(IdcardUtil.getGenderByIdCard(familyPersonVo.getIdCard()));
                //身份证为空且医学出生证明不为空
            }else if(ObjectUtil.isEmpty(familyPersonVo.getIdCard()) && ObjectUtil.isNotEmpty(familyPersonVo.getBirthCertificate())){
                userInfo.setBirthCertificate(familyPersonVo.getBirthCertificate());
                //身份证为空,则判断出生医学证
                UserInfo userByBirthCertificate = userInfoMapper.selectByBirthCertificate(familyPersonVo.getBirthCertificate());
                //出生医学证查询存在则直接返回
                //出生医学不为空则直接返回
                if (BeanUtil.isNotEmpty(userByBirthCertificate)){
                    BeanUtil.copyProperties(userByBirthCertificate,userInfo);
                    return userInfo;
                }
                userInfo.setBirthday(familyPersonVo.getBirthday());
                userInfo.setGender(1);
            }else {
                //身份证为空且医学出生证明为空
                return userInfo;
            }
            //设置其他信息
            userInfo.setGravid(familyPersonVo.getGravid());
            userInfo.setLastMenstruation(familyPersonVo.getLastMenstruation());
            userInfo.setUserName(familyPersonVo.getUserName());
            userInfo.setDeptId(familyPersonVo.getDeptId());
            userInfo.setAreaName(familyPersonVo.getAreaName());
            userInfo.setLoginUserName(familyPersonVo.getUserName());
            userInfo.setCreateTime(DateUtil.date());
            //设置密码
            userInfo.setPassword(SaltMD5Util.getEncryptedPwd(familyPersonVo.getMobile()));
            userInfo.setSalt(SaltMD5Util.SALT);
            //把添加者的信息赋给家庭成员
            if (ObjectUtil.isEmpty(familyPersonVo.getMobile())){
                //将添加者的手机号给被添加者
                userInfo.setPhone(userFrom.getPhone());
            }else {
                //将自己的登录手机号作为联系手机号
                userInfo.setPhone(familyPersonVo.getMobile());
            }
            userInfo.setRelateTime(DateUtil.date());
            userInfo.setIsPush("1");
            userInfo.setStatus("1");

        }
        return userInfo;
    }

    @Override
    @Transactional
    public AjaxResult editById(UserInfo userInfo) {
        userInfo.setLoginUserName(userInfo.getUserName());
        userInfo.setUpdateTime(DateUtil.date());
        userInfo.setBirthday(IdcardUtil.getBirthDate(userInfo.getIdCard()));
        userInfo.setGender(IdcardUtil.getGenderByIdCard(userInfo.getIdCard()));
        //设置联系方式
        userInfo.setPhone(userInfo.getPhone());
        if(userInfoMapper.updateUserById(userInfo) && editArchives(userInfo)){
            return AjaxResult.success("修改成功");
        }else {
            return AjaxResult.error("修改失败");
        }
    }

    private Boolean editArchives(UserInfo userInfo) {
        Boolean isSuccess = false;
        ArchivesVo archivesVo = new ArchivesVo();
        BeanUtil.copyProperties(userInfo, archivesVo);
        archivesVo.setUpdateUser(userInfo.getUserName());
        archivesVo.setUserId(userInfo.getId());
        archivesVo.setId(null);
        Integer age = IdcardUtil.getAgeByIdCard(userInfo.getIdCard());
        return isSuccessForEditArchives(userInfo, isSuccess, archivesVo, age);


    }

    private Boolean isSuccessForEditArchives(UserInfo userInfo, Boolean isSuccess, ArchivesVo archivesVo, Integer age) {
        if (age >= 7 && age <= 65){
            if(userInfo.getGender() == 0 && userInfo.getGravid() == 1){
                //孕妇
                archivesVo.setIsGravida(userInfo.getGravid());
                archivesVo.setFinalMenstruationTime(DateUtil.format(userInfo.getLastMenstruation(),"yyyy-MM-dd"));
                Long gestationalAge = DateUtil.betweenWeek(userInfo.getLastMenstruation(), DateUtil.date(), false);
                archivesVo.setGestationalAge(Integer.parseInt(gestationalAge.toString()));
                archivesVo.setGestationalPeriod(DateUtil.offsetDay(userInfo.getLastMenstruation(), 280));
                isSuccess =  archivesGravidaService.editArchivesGravida(archivesVo);
            }else {
                //普通人
                isSuccess =  archivesCommonService.editArchivesCommon(archivesVo);
            }
        }else if (age > 65){
            //老年人
            isSuccess =  archivesOldService.editArchivesOld(archivesVo);
        }else if (age < 7){
            //儿童
            isSuccess =  archivesChildService.editArchivesChild(archivesVo);
        }
        return isSuccess;
    }

    @Override
    public UserInfo getOneById(Long userId) {
        return userInfoMapper.selectOneById(userId);
    }

    /**
     * 微信登录，获取sessionKey和openid
     * @param wxLoginBodyVo
     * @return
     * @throws Exception
     */
    @Override
    public Map wxLogin(WxLoginBodyVo wxLoginBodyVo) throws Exception {
        Map<String, Object> map = new HashMap<>();
        //获取openId 和 sessionKey
        String res = this.getOpenId(wxLoginBodyVo.getUserInfoCode());
        cn.hutool.json.JSONObject resEntries = JSONUtil.parseObj(res);
        String openId = resEntries.getStr("openid");
        String sessionKey = resEntries.getStr("session_key");
        if (StringUtils.isEmpty(openId)) {
            map.put("msg", "未获取到openId，登录失败");
            return map;
        }
        //解密.获取加密信息
        String data = WxDecryptUtills.decrypt(wxLoginBodyVo.getEncryptedData(), sessionKey, wxLoginBodyVo.getEncryptedIv());
//        String data = DecryptUtil.decryptData(wxLoginBody.getEncryptedData(), wxLoginBody.getEncryptedIv(), sessionKey);
        cn.hutool.json.JSONObject dataEntries = JSONUtil.parseObj(data);
        Console.log("加密信息",dataEntries);
        map.put("nickName", dataEntries.getStr("nickName"));
        map.put("gender", dataEntries.getStr("gender"));
        map.put("avatarUrl", dataEntries.getStr("avatarUrl"));
        return map;
    }
    /**
     * 手机号登录
     * @param wxLoginUserInfoVo
     * @return
     */
    @Override
    public AjaxResult wxLoginByPhone(WxLoginUserInfoVo wxLoginUserInfoVo) {
        //获取用户手机号
        String phoneNumber = this.getPhoneNumber(wxLoginUserInfoVo.getPhoneCode());
        if (StringUtils.isEmpty(phoneNumber)) {
            return AjaxResult.error("未获取到手机号，登录失败");
        }
        //通过手机号查询用户信息
        UserInfo user = selectUserByPhone(phoneNumber);
        //从档案里面查
        ArchivesVo archivesVo = new ArchivesVo();
        if(BeanUtil.isNotEmpty(user)){
            archivesVo = getArchive(user);
        }

        SysUser sysUser = new SysUser();
        //没有,则新增
//        KelinUserInfoBody kelinUserInfoBody = new KelinUserInfoBody();
        if (BeanUtil.isEmpty(user) && BeanUtil.isEmpty(archivesVo)) {
            return AjaxResult.success("该账号未注册").put("mobile", phoneNumber);
//            UserInfo userInfo = creatUser(phoneNumber);
//            //数据库中新增用户
//            userInfoMapper.insertUser(userInfo);
//            BeanUtil.copyProperties(userInfo, sysUser.getKelinUserInfoBody());
//            sysUser.setUserId(sysUser.getKelinUserInfoBody().getId());
        }else if(BeanUtil.isNotEmpty(user) && BeanUtil.isNotEmpty(archivesVo)){
            BeanUtil.copyProperties(user,sysUser.getKelinUserInfoBody());
            sysUser.setUserId(sysUser.getKelinUserInfoBody().getId());
        }
        //生成token
        String token = getToken(phoneNumber, sysUser);
        return AjaxResult.success().put(Constants.TOKEN, token);
    }

    private ArchivesVo getArchive(UserInfo user) {
        ArchivesVo archivesVo = new ArchivesVo();
        if(UserType.COMMON.equals(user.getUserType())){
            archivesVo = archivesCommonService.getUserArchivesCommonByUserId(user.getId());
        }else if(UserType.CHILDREN.equals(user.getUserType())){
            archivesVo = archivesChildService.getUserArchivesChrByUserId(user.getId());
        }else if(UserType.GRAVIDA.equals(user.getUserType())){
            archivesVo = archivesGravidaService.getUserArchivesGravidaByUserId(user.getId());
        }else if(UserType.OLD.equals(user.getUserType())){
            archivesVo = archivesOldService.getUserArchivesOldByUserId(user.getId());
        }
        return archivesVo;
    }



    /**
     * 根据userId登录
     * @param userId
     * @return
     */
    @Override
    public String wxLoginByUserId(Long userId) {
        UserInfo user = selectUserByUserId(userId);
        if (BeanUtil.isNotEmpty(user)){
            SysUser sysUser = new SysUser();
            BeanUtil.copyProperties(user, sysUser.getKelinUserInfoBody());
            if (BeanUtil.isNotEmpty(user.getPhone())){
                //生成token
                String token = getToken(user.getPhone(), sysUser);
                return token;
            }
        }
        return null;
    }

    @Override
    public AjaxResult addFamilyMemberInfo(FamilyPersonVo familyPersonVo) {
        UserInfo userInfo = new UserInfo();
        BeanUtil.copyProperties(familyPersonVo, userInfo);
       if (userInfoMapper.updateUserById(userInfo)){
           return AjaxResult.success("添加成功");
       }
        return AjaxResult.error("添加失败");
    }

    @Override
    @Transactional
    public AjaxResult addWxLoginUserInfo(UserInfo wxLoginUserInfo) {
        UserInfo userInfo = userInfoMapper.selectOneByIdCard(wxLoginUserInfo.getIdCard());
        if (BeanUtil.isNotEmpty(userInfo)){
            return AjaxResult.error("身份证号已被注册,请重新输入");
        }

        UserInfo wxLoginUser = getWxLoginUserInfo(wxLoginUserInfo);
        //新增用户信息

        if(userInfoMapper.insertUser(wxLoginUser)){
            //新增档案信息
            if(addArchives(wxLoginUser)){
                SysUser sysUser = new SysUser();
                BeanUtil.copyProperties(wxLoginUser,sysUser.getKelinUserInfoBody());
                sysUser.setUserId(sysUser.getKelinUserInfoBody().getId());
                String token = getToken(wxLoginUser.getMobile(), sysUser);
                return AjaxResult.success().put(Constants.TOKEN, token);
            }
        }

        return AjaxResult.error("登录失败");
    }

    /**
     * 添加家庭成员档案
     * @param user
     * @param userFrom
     * @return
     */
    private Boolean addFamilyArchives(UserInfo user,UserInfo userFrom) {
        Boolean isSuccess = false;
        ArchivesVo archivesVo = new ArchivesVo();
        BeanUtil.copyProperties(user,archivesVo);
        archivesVo.setCreateUser(userFrom.getUserName());
        archivesVo.setBirthday(DateUtil.format(user.getBirthday(),"yyyy-MM-dd"));
        archivesVo.setUserId(user.getId());
        archivesVo.setId(null);

        Integer age = DateUtil.ageOfNow(user.getBirthday());
        return isSuccessForAddArchives(user, isSuccess, archivesVo, age);
    }

    /**
     * 添加档案
     * @param wxLoginUser
     * @return
     */

    private Boolean addArchives(UserInfo wxLoginUser) {
        Boolean isSuccess = false;
        ArchivesVo archivesVo = new ArchivesVo();
        BeanUtil.copyProperties(wxLoginUser,archivesVo);
        archivesVo.setCreateUser(wxLoginUser.getUserName());
        archivesVo.setBirthday(DateUtil.format(wxLoginUser.getBirthday(),"yyyy-MM-dd"));
        archivesVo.setUserId(wxLoginUser.getId());
        archivesVo.setId(null);

        Integer age = DateUtil.ageOfNow(wxLoginUser.getBirthday());
        return isSuccessForAddArchives(wxLoginUser, isSuccess, archivesVo, age);
    }

    private Boolean isSuccessForAddArchives(UserInfo wxLoginUser, Boolean isSuccess, ArchivesVo archivesVo, Integer age) {
        if (age >= 7 && age <= 65){

            if(wxLoginUser.getGender() == 0 && wxLoginUser.getGravid() == 1){
                //孕妇
                archivesVo.setIsGravida(wxLoginUser.getGravid());
                archivesVo.setFinalMenstruationTime(DateUtil.format(wxLoginUser.getLastMenstruation(),"yyyy-MM-dd"));
                Long gestationalAge = DateUtil.betweenWeek(wxLoginUser.getLastMenstruation(), DateUtil.date(), false);
                archivesVo.setGestationalAge(Integer.parseInt(gestationalAge.toString()));
                archivesVo.setGestationalPeriod(DateUtil.offsetDay(wxLoginUser.getLastMenstruation(), 280));

                if(archivesGravidaService.getUserArchivesGravidaByUserId(wxLoginUser.getId()) !=null){
                    isSuccess = true;
                }else {
                    isSuccess =  archivesGravidaService.addArchivesGravida(archivesVo);
                }
            }else {
                //普通人
                if(archivesCommonService.getUserArchivesCommonByUserId(wxLoginUser.getId()) != null){
                    isSuccess = true;
                }else {
                    isSuccess =  archivesCommonService.addArchivesCommon(archivesVo);
                }
            }

        }else if (age > 65){
            //老年人
            if(archivesOldService.getUserArchivesOldByUserId(wxLoginUser.getId()) != null){
                isSuccess = true;
            }else {
                isSuccess =  archivesOldService.addArchivesOld(archivesVo);
            }
        }else if (age < 7){
            //儿童
            if(archivesChildService.getUserArchivesChrByUserId(wxLoginUser.getId()) != null){
                isSuccess = true;
            }else {
                isSuccess =  archivesChildService.addArchivesChild(archivesVo);
            }
        }
        return isSuccess;
    }

    private UserInfo getWxLoginUserInfo(UserInfo wxLoginUserInfo) {
        UserInfo wxLoginUser = new UserInfo();
        BeanUtil.copyProperties(wxLoginUserInfo,wxLoginUser);

        DateTime birthDate = IdcardUtil.getBirthDate(wxLoginUserInfo.getIdCard());
        wxLoginUser.setBirthday(birthDate);
        wxLoginUser.setLoginUserName(wxLoginUserInfo.getUserName());
        wxLoginUser.setPassword(SaltMD5Util.getEncryptedPwd(wxLoginUserInfo.getMobile()));
        wxLoginUser.setSalt(SaltMD5Util.SALT);
        wxLoginUser.setCreateTime(DateUtil.date());
        wxLoginUser.setPhone(wxLoginUserInfo.getMobile());
        wxLoginUser.setStatus("1");
        return wxLoginUser;
    }

    private UserInfo selectUserByUserId(Long userId) {
        UserInfo user = userInfoMapper.selectOneById(userId);
        String userType = null;
        if (BeanUtil.isEmpty(user)){
            return null;
        }
        Integer age = DateUtil.ageOfNow(user.getBirthday());
        if (age >= 7 && age <= 65){

            if(user.getGender() == 0 && user.getGravid() == 1){
                //孕妇
                userType = UserType.GRAVIDA;
            }else {
                //普通人
                userType = UserType.COMMON;
            }

        }else if (age > 65){
            //老年人
            userType = UserType.OLD;
        }else if (age < 7){
            //儿童
            userType = UserType.CHILDREN;
        }
        user.setUserType(userType);
        return user;
    }

    //生成token
    private String getToken(String phoneNumber, SysUser sysUser) {
        LoginUser loginUser = new LoginUser();
        loginUser.setPhoneNumber(phoneNumber);
        loginUser.setUser(sysUser);
        loginUser.setUserId(sysUser.getKelinUserInfoBody().getId());
        String token = tokenService.createToken(loginUser);
        return token;
    }
    //生成userInfo
    private UserInfo selectUserByPhone(String phoneNumber) {
        UserInfo user = userInfoMapper.selectUserByPhone(phoneNumber);
        String userType = null;
        if (BeanUtil.isEmpty(user)){
            return null;
        }
        Integer age = DateUtil.ageOfNow(user.getBirthday());
        if (age >= 7 && age <= 65){
            if (user.getGender()==0 && user.getGravid()==1){
                //孕妇
                userType = UserType.GRAVIDA;
            }else {
                //普通人
                userType = UserType.COMMON;
            }

        }else if (age > 65){
            //老年人
            userType = UserType.OLD;
        }else if (age < 7){
            //儿童
            userType = UserType.CHILDREN;
        }
        user.setUserType(userType);
        return user;
    }


    /**
     * 获取accesstoken
     *
     * @return
     */
    private String getAccessToken() {
        String accessToken = "";
        String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + weixinProperties.getAppid() + "&secret=" + weixinProperties.getSecret();
        try {
            String resultString = HttpUtil.get(url);
            Console.log("获取微信accessToken:{}", resultString);
            if (StringUtils.isNotEmpty(resultString)) {
                JSONObject jsonObject = JSON.parseObject(resultString);
                accessToken = jsonObject.get("access_token").toString();
            } else {
                log.error("返回值为空，请检查请求报文或者请求地址是否正确");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return accessToken;
    }

    /**
     * 获取手机号
     */
    private String getPhoneNumber(String code) {
        String phoneNumber = "";
        String url = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=" + getAccessToken();
        JSONObject jsonCode = new JSONObject();
        jsonCode.put("code",code);
        try {
            String resultString = HttpUtil.post(url, jsonCode.toString());
            Console.log("获取微信手机号码：{}", resultString);
            if (StringUtils.isNotEmpty(resultString)) {
                JSONObject jsonObject = JSON.parseObject(resultString);
                JSONObject phone_info = jsonObject.getJSONObject("phone_info");
                phoneNumber = phone_info.getString("phoneNumber");
            } else {
                log.error("返回值为空，请检查请求报文或者请求地址是否正确");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return phoneNumber;
    }

    /**
     * 获取openId
     */
    private String getOpenId(String code) {
        //https://api.weixin.qq.com/sns/jscode2session?appid=APPID&secret=SECRET&js_code=JSCODE&grant_type=authorization_code
        String url = "https://api.weixin.qq.com/sns/jscode2session?appid="
                +weixinProperties.getAppid()+"&secret="
                +weixinProperties.getSecret()+"&js_code="
                +code+"&grant_type=authorization_code";
        try {
            String resultString = HttpUtil.get(url);
            Console.log("获取微信openId:{}", resultString);
            if (StringUtils.isNotEmpty(resultString)) {
                return resultString;
            } else {
                log.error("返回值为空，请检查请求报文或者请求地址是否正确");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
    /**
     * 生成sysUser
     * @param user
     * @param
     * @return
     */
    private SysUser creatSysUser(UserInfo user ) {
        SysUser sysUser = new SysUser();
        sysUser.setNickName(user.getUserName());
        sysUser.setPassword(sysUser.getPassword());
        sysUser.setPhonenumber(user.getPassword());
        sysUser.setCreateTime(new DateTime());
        sysUser.setDelFlag("0");
        sysUser.setStatus("0");
        sysUser.setLoginDate(new Date());
        return sysUser;
    }

    /**
     * 生成用户
     * @param
     * @return
     */
    private UserInfo creatUser(String phoneNumber ) {
        UserInfo userInfo = new UserInfo();
            //设置登录手机号
            userInfo.setMobile(phoneNumber);
            //设置手机号
            userInfo.setPhone(phoneNumber);
            //密码
            userInfo.setPassword(SaltMD5Util.getEncryptedPwd(phoneNumber));
            userInfo.setSalt(SaltMD5Util.SALT);
            //创建时间
            userInfo.setCreateTime(DateUtil.date());
            //是否有效
            userInfo.setStatus("1");

        return userInfo;
    }
}
